package com.shop.controller;

import com.shop.domain.ExtCproduct;
import com.shop.domain.Factory;
import com.shop.domain.SysCode;
import com.shop.service.ExtCproductService;
import com.shop.service.FactoryService;
import com.shop.service.SysCodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ExtProductController
 * Created by heqianqian on 2017/7/18.
 */
@Controller
@RequestMapping("/cargo/product/ext")
public class ExtProductController {

    @Resource
    private FactoryService<Factory> factoryService;

    @Resource
    private ExtCproductService<ExtCproduct> extCproductService;

    @Resource
    private SysCodeService<SysCode> sysCodeSysCodeService;

    /**
     * 跳转到创建附件页面
     */
    @RequestMapping("/create")
    public String create(@RequestParam("contractProId") Long contractProId, Model model) {
        //设置货物Id
        model.addAttribute("contractProId", contractProId);
        //设置工厂列表
        List<Factory> factories = factoryService.listFactory();
        model.addAttribute("factoryList", factories);
        //设置附件类型
        List<SysCode> ctypeList = sysCodeSysCodeService.getCtypeList();
        model.addAttribute("ctypeList", ctypeList);
        //设置某个货物下的附件信息
        Map<String, Object> map = new HashMap<>();
        map.put("contractProId", contractProId);
        List<ExtCproduct> dataList = extCproductService.find(map);
        model.addAttribute("dataList", dataList);
        //跳转到创建附件页面
        return "/cargo/product/ext/ext_product_create";
    }

    /**
     * 插入附件
     */
    @RequestMapping("/insert")
    public String insert(ExtCproduct extCproduct, Model model) {
        extCproductService.insert(extCproduct);
        model.addAttribute("contractProId", extCproduct.getContractProId());
        return "redirect:/cargo/product/ext/create";
    }

    /**
     * 跳转到修改页面
     */
    @RequestMapping("/modify")
    public String modify(@RequestParam("extCproductId") Long extCproductId, Model model) {
        //设置当前附件信息
        ExtCproduct extCproduct = extCproductService.finById(extCproductId);
        model.addAttribute("obj", extCproduct);
        //添加工厂信息
        List<Factory> factories = factoryService.listFactory();
        model.addAttribute("factoryList", factories);
        //添加附件信息
        List<SysCode> ctypeList = sysCodeSysCodeService.getCtypeList();
        model.addAttribute("ctypeList", ctypeList);
        //跳转到修改页面
        return "cargo/product/ext/ext_product_update";
    }

    /**
     * 更新附件信息
     */
    @RequestMapping("/update")
    public String update(ExtCproduct extCproduct, Model model) {
        extCproductService.update(extCproduct);
        model.addAttribute("contractProId", extCproduct.getContractProId());
        return "redirect:/cargo/product/ext/create";
    }

    /**
     * 删除附件信息
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam("extCproductId") Long extCproductId,
                         @RequestParam("contractProId") Long contractProId,
                         Model model) {
        extCproductService.deleteById(extCproductId);
        model.addAttribute("contractProId", contractProId);
        return "redirect:/cargo/product/ext/create";
    }

}
